//
//  DiscoveryTableViewController.m
//  MagazineShangejie
//
//  Created by mac on 15/4/21.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "DiscoveryViewController.h"

#import "DiscoveryTableView.h"
#import "Util.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface DiscoveryViewController ()<UIScrollViewDelegate,UITextViewDelegate>{
    UISegmentedControl*segmented;
    UIView*FooterView;
    UIButton *closeKeyboardButton;
    float keyHeight;
}
@property (strong,nonatomic)UITextView*textView;
@property (strong,nonatomic)UIScrollView*mainScroller;
@property (strong,nonatomic)NSString*ID;
@property (strong,nonatomic)NSString*cateid;
@end

@implementation DiscoveryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initScroller];
    [self initSegmented];
    [self initCommentField];
    [self registerForKeyboardNotifications];
}

-(void)viewWillDisappear:(BOOL)animated{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self closeKeyboard];
}
-(void)closeKeyboard{
    [self.textView resignFirstResponder];
}
-(void)initScroller{
    self.mainScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenW , self.view.frame.size.height-95)];
    self.mainScroller.showsHorizontalScrollIndicator = NO;
    self.mainScroller.showsVerticalScrollIndicator = NO;
    self.mainScroller.pagingEnabled = YES;
    self.mainScroller.backgroundColor = [UIColor grayColor];
    self.mainScroller.bounces = NO;
    self.mainScroller.delegate = self;
    self.mainScroller.contentSize = CGSizeMake(kScreenW*2,self.mainScroller.frame.size.height);
    [self.view insertSubview:self.mainScroller atIndex:0];
    __weak typeof(self) unself = self;
    
    UIStoryboard*story=self.storyboard;
    DiscoveryTableView *scroller = [story instantiateViewControllerWithIdentifier:@"DiscoveryTableView"];
    scroller.view.frame=CGRectMake(0 * self.mainScroller.frame.size.width, 0, self.mainScroller.frame.size.width, self.mainScroller.frame.size.height);
    
    scroller.DiscoveryCommentBlcok=^(NSString*ID,NSString*cateid){
        unself.ID=ID;unself.cateid=cateid;
        [unself.textView becomeFirstResponder];
    };
    [scroller setURLString:@"System/Found"];
    [self addChildViewController:scroller];
    [self.mainScroller addSubview:scroller.view];
    
    DiscoveryTableView *scroller2 = [story instantiateViewControllerWithIdentifier:@"DiscoveryTableView"];
    scroller2.view.frame=CGRectMake(1 * self.mainScroller.frame.size.width, 0, self.mainScroller.frame.size.width, self.mainScroller.frame.size.height);
    [scroller2 setURLString:@"System/Hot"];
    scroller2.DiscoveryCommentBlcok=^(NSString*ID,NSString*cateid){
        unself.ID=ID;unself.cateid=cateid;
        [unself.textView becomeFirstResponder];
    };
    [self addChildViewController:scroller2];
    [self.mainScroller addSubview:scroller2.view];
}
-(void)initCommentField{
    closeKeyboardButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [closeKeyboardButton addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
    [closeKeyboardButton setFrame:self.view.frame];
    [closeKeyboardButton setBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.300]];
    [closeKeyboardButton setAlpha:0];
    [self.view addSubview:closeKeyboardButton];
    FooterView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.height, 145)];
    FooterView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:FooterView];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.5)];
    label.backgroundColor=UIColorFromRGB(0xcccccc);
    [FooterView addSubview:label];///分割线
    self.textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, 75)];
    UILabel * leftView3 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,10,26)];
    leftView3.backgroundColor = [UIColor clearColor];
    self.textView.delegate=self;
    self.textView.tag=123;
//    self.textView.returnKeyType=UIReturnKeyDone;
    self.textView.backgroundColor=[UIColor colorWithWhite:0.969 alpha:1.000];
    self.textView.layer.borderWidth=0.5;
    self.textView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    
    [FooterView addSubview:self.textView];
    UILabel*signature=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 30)];
    signature.textColor=[UIColor blackColor];
    signature.textAlignment=NSTextAlignmentCenter;
    signature.text=@"说两句";
    [FooterView addSubview:signature];
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(self.view.frame.size.width-50, 10, 40, 30)];
//    [button setBackgroundColor:[UIColor redColor]];
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button.layer.cornerRadius = 5;
//    button.layer.masksToBounds = YES;
    [button addTarget:self action:@selector(AddComment:) forControlEvents:UIControlEventTouchUpInside];
    [FooterView addSubview:button];
    
    UIButton*button2=[UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setFrame:CGRectMake(10, 10, 40, 30)];
    [button2 setTitle:@"取消" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
    [FooterView addSubview:button2];
}
-(void)initSegmented{
    segmented=[[UISegmentedControl alloc]initWithItems:@[@"发现", @"推荐"]];
    segmented.frame=CGRectMake(self.view.frame.size.width/2-segmented.frame.size.width/2, 25, 140, 30);
    segmented.tintColor=[UIColor redColor];
    segmented.selectedSegmentIndex=0;
    
    segmented.backgroundColor=[UIColor clearColor];
    segmented.layer.borderColor=[UIColor redColor].CGColor;
    segmented.layer.borderWidth=1;
    [segmented.layer setMasksToBounds:YES];
    segmented.layer.cornerRadius = CGRectGetHeight(segmented.frame) / 2.0f;
    [segmented addTarget:self action:@selector(SegmentedAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView=segmented;
}
#pragma mark - 评论相关代码

-(void)AddComment:(id)sender{
    if (self.textView.text.length<3) {
        [SVProgressHUD showErrorWithStatus:@"评论过短"];
        return;
    }
    __weak typeof(self) unself = self;
    [SVProgressHUD show];
    [[AppAPIClient sharedClient] POST:@"Comment/Add" parameters:@{@"id":self.ID,@"cateid":self.cateid,@"content":self.textView.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        unself.textView.text=@"";
        [SVProgressHUD showSuccessWithStatus:@"留言成功"];
        [unself closeKeyboard];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ERR: %@",operation.responseString);
        [SVProgressHUD showErrorWithStatus:@"评论失败"];
    }];
}
-(void)SegmentedAction:(id)obj{
    [self.textView resignFirstResponder];
    UISegmentedControl*segme=(UISegmentedControl*)obj;
    [UIView animateWithDuration:.3 animations:^{
       self.mainScroller.contentOffset =  CGPointMake(segme.selectedSegmentIndex * self.mainScroller.frame.size.width, 0);
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - textView Delegate

-(void)textViewDidBeginEditing:(UITextView *)textView{
    [UIView animateWithDuration:.3 animations:^{
        [closeKeyboardButton setAlpha:1];
    }];
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    [UIView animateWithDuration:.3 animations:^{
        [closeKeyboardButton setAlpha:0];
        FooterView.frame=CGRectMake(0, self.view.frame.size.height, FooterView.frame.size.width, FooterView.frame.size.height);

    }];
}


- (void) registerForKeyboardNotifications

{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}


- (void)keyboardWasShown:(NSNotification *) notif

{
    
    NSDictionary *info = [notif userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGSize keyboardSize = [value CGRectValue].size;
    
    if (keyboardSize.height>keyHeight) {
        keyHeight=keyboardSize.height-50;

    }
    
    NSLog(@"keyBoard:%f", keyboardSize.height);  //216
    [UIView animateWithDuration:.3 animations:^{
                FooterView.frame=CGRectMake(0, self.view.frame.size.height-keyHeight-FooterView.frame.size.height, FooterView.frame.size.width, FooterView.frame.size.height);
        
    }];
    ///keyboardWasShown = YES;
    
}

- (void) keyboardWasHidden:(NSNotification *) notif

{
    
    NSDictionary *info = [notif userInfo];
    
    
    
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    
    CGSize keyboardSize = [value CGRectValue].size;
    
    NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.height);
    [UIView animateWithDuration:.3 animations:^{
        [closeKeyboardButton setAlpha:0];
        FooterView.frame=CGRectMake(0, self.view.frame.size.height, FooterView.frame.size.width, FooterView.frame.size.height);
        
    }];
    
    
    
}
#pragma mark - scrollView
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [UIView animateWithDuration:0.3 animations:^{
        segmented.selectedSegmentIndex =scrollView.contentOffset.x / self.mainScroller.frame.size.width;
    }];
    
//    [segmented itemClickByScrollerWithIndex:scrollView.contentOffset.x / self.mainScroller.frame.size.width];
}


@end
